package com.imooc.firstappdemo.controller;

import com.imooc.firstappdemo.domain.User;
import com.imooc.firstappdemo.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 *
 *
 * @author caojl
 * @create 2018/03/20
 **/
@RestController
public class UserController {


    private final UserRepository userRepository;

    /**
     * PS：课程讲到这里时，再次回顾的构造器的注入方式的好处
     * @param userRepository
     */
    @Autowired
    public UserController(UserRepository userRepository){
        this.userRepository = userRepository;
    }

    @PostMapping("/person/save")
    public User save(@RequestParam  String name){
        User user = User.builder().name(name).build();
        if(userRepository.save(user)){
            System.out.printf("用户对象：%s 保存成功！\n", user);
        }
        return user;
    }
}
